<?php

namespace app\store\controller;

use think\Request;
use app\store\model\ProjectTasks as ProjectTasksModel;
use app\store\model\Material;
use app\store\model\UserInfo;

class ProjectTasks extends Controller
{
    public $model;
    public $material;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new ProjectTasksModel();
        $this->material = new Material();
    }

    /**
     * 列表
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function index(){
        $param = $this->request->param();
        $list = $this->model->getList($param);
        return show(self::SUCCESS,'操作成功',$list);
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function add(){
        $data = $this->request->param();
       if(!empty($data['user_id'])){
            $data['dispatch_time'] = date('Y-m-d H:i:s',time());
        }
        if(!empty($data['material'])){
            $material = json_decode(htmlspecialchars_decode($data['material']),true);
            unset($data['material']);
        }
        $result = $this->model->save($data);
        if($result && !empty($material) && is_array($material)){
            $this->material->add($material,$this->model->id);
        }
        if($result){
            return show(self::SUCCESS,'操作成功');
        }
        return show(self::ERROR,'操作失败');
    }

    /**
     * 更新
     * @param $id
     * @return \think\response\Json
     */
    public function edit($id){
        $data = $this->request->param();
        if(!empty($data['entry_type_name'])){
            unset($data['entry_type_name']);
        }
        if(!empty($data['material'])){
            $material = json_decode(htmlspecialchars_decode($data['material']),true);
            unset($data['material']);
        }
        $result = $this->model->edit($data,$id);
        if($result && !empty($material) && is_array($material)){
            $this->material->add($material,$this->model->id);
        }
        if($result){
            return show(self::SUCCESS,'操作成功');
        }
        return show(self::ERROR,'操作失败');
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id){
        $result = ProjectTasksModel::del($id);
        if($result){
            Material::destroy(['tasks_id'=>$id]);
        }
        if($result){
            return show(self::SUCCESS,'操作成功');
        }
        return show(self::ERROR,'操作失败');
    }

    /**
     * 项目类型列表
     * @return \think\response\Json
     */
    public function projectType(){
        $data = entryType();
        return show(self::SUCCESS,'操作成功',$data);
    }

    /**
     * 财务信息
     * @return \think\response\Json
     */
    public function financeType(){
        $data = financeType();
        return show(self::SUCCESS,'操作成功',$data);
    }

    /**
     * 审核订单
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function examine($id){
        $detail = $this->model->find($id);
        $detail->end_time = date('Y-m-d H:i:s',time());
        $detail->entry_status = ProjectTasksModel::REVIEWED;
        $result = $detail->save();
        if($result){
            return show(self::SUCCESS,'操作成功');
        }
        return show(self::ERROR,'操作失败');
    }

    /**
     * 获取审核列表【
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function examineList(){
        $company_id = $this->request->param('company_id');
        $entry_name = $this->request->param('entry_name');
        if(empty($company_id)){
            return show(self::ERROR,'公司id不允许为空');
        }
        $data = $this->model->getExamineList($company_id,$entry_name);
        return show(self::SUCCESS,'操作成功',$data);
    }

    /**
     * 首页统计
     * @return \think\response\Json
     */
    public function statistics(){
        $data = $this->model->statistics();
        return show(self::SUCCESS,'操作成功',$data);
    }

    /**
     * 获取用户列表
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function userList(){
        $param = $this->request->param();
        $where = [];
        if(!empty($param['company_id'])){
            $where['company_id'] = $param['company_id'];
        }
        $user = new UserInfo();
        $data = $user->where($where)->select();
        return show(self::SUCCESS,'操作成功',$data);
    }

    /**
     * 订单统计列表
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function statisticsOrder(){
        /*if(empty($this->store)){
            return show(self::ERROR,'您还没有登陆');
        }*/
        $param = $this->request->param();
        $data = $this->model->orderStatistics($param);
        return show(self::SUCCESS,'操作成功',$data);
    }

    /**
     * 详情信息
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id){
        $result = $this->model->detail($id);
        if($result){
            return show(self::SUCCESS,'操作成功',$result);
        }
        return show(self::ERROR,'操作失败');
    }



}